package com.yijiu.trb;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BaseResponse;

/**
 * 绑定或者修改手机
 * 
 * @author TangChao
 */
public class BindToPhoneActivity extends BTitleActivity implements OnClickListener
{
	/**
	 * 新手机号码编辑框
	 */
	private EditText newPhoneNum;

	/**
	 * 验证码编辑框
	 */
	private EditText phoneCode;

	/**
	 * 旧手机号码编辑框
	 */
	private TextView oldPhoneNum;

	/**
	 * 新手机号码
	 */
	private String newMobile;

	/**
	 * 旧手机号码
	 */
	private String oldMobile;

	/**
	 * 发送手机验证码的按钮
	 */
	private TextView sendCode;

	/**
	 * 上一个页面传过来用于标识是修改手机还是绑定手机
	 */
	private String flag;

	/**
	 * 用于显示是修改手机还是绑定手机
	 */
	private String flagStr;

	/**
	 * 接口传递的参数值
	 */
	private String flagServer;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_to_phone_layout);
		initWidget();
	}

	@Override
	protected void initWidget()
	{
		flag = getIntent().getStringExtra("flag");
		if ("change".equals(flag))// 修改手机
		{
			flagStr = "修改";
			flagServer = "NEWCELLPHONE";
		}
		else
		// 绑定手机
		{
			flagStr = "绑定";
			flagServer = "ADDCELLPHONE";
		}
		titleView.setText(flagStr + "手机");
		sendCode = (TextView) findViewById(R.id.btn_bind_to_phone_code_send);
		sendCode.setOnClickListener(this);
		Button submit = (Button) findViewById(R.id.btn_bind_to_phone_code_submit);
		submit.setOnClickListener(this);
		oldPhoneNum = (TextView) findViewById(R.id.tv_bind_to_phone_num);
		if (StringUtils.isNull(CacheData.MOBILE_NUMBER))
		{
			oldPhoneNum.setVisibility(View.GONE);
		}
		else
		{
			oldPhoneNum.setVisibility(View.VISIBLE);
			oldPhoneNum.setText(CacheData.MOBILE_NUMBER);
		}
		newPhoneNum = (EditText) findViewById(R.id.et_bind_to_phone_num_again);
		phoneCode = (EditText) findViewById(R.id.et_bind_to_phone_code);
	}

	/**
	 * 发送验证码
	 */
	private void doSendVerification()
	{
		oldMobile = oldPhoneNum.getText().toString();
		newMobile = newPhoneNum.getText().toString();
		if (StringUtils.isNull(newMobile))
		{
			app.showMsg("请输入手机号码");
			return;
		}
		if (!StringUtils.isPhoneNumberValid(newMobile))
		{
			app.showMsg("请输入有效的手机号码");
			return;
		}
		timerCount(sendCode);
		String inputStr = "service=appMobileService&mobile=" + (StringUtils.isNull(oldMobile) ? newMobile : oldMobile)
				+ "&business=" + flagServer;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.SEND_AGAIN);
		netManager.excute(r, this, this);
		showProgress("正在发送验证码，请稍候！");
	}

	/**
	 * 绑定手机
	 */
	private void doBindToPhone()
	{
		String codeStr = phoneCode.getText().toString();
		newMobile = newPhoneNum.getText().toString();
		oldMobile = oldPhoneNum.getText().toString();
		if (StringUtils.isNull(codeStr))
		{
			app.showMsg("请输入手机验证码！");
			return;
		}
		String inputStr = null;
		if (StringUtils.isNull(oldMobile))
		{
			inputStr = "service=appBindingMobileService&verifyCode=" + codeStr + "&mobile=" + newMobile + "&business="
					+ flagServer;
		}
		else
		{
			inputStr = "service=appBindingMobileService&verifyCode=" + codeStr + "&mobile=" + oldMobile + "&newMobile="
					+ newMobile + "&business=" + flagServer;
		}
		Log.e("result", inputStr);
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.BIND_CHANGE_PHONE);
		netManager.excute(r, this, this);
		showProgress("正在" + flagStr + "手机，请稍候！");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.SEND_AGAIN:// 发送验证码
				app.showMsg("发送验证码成功！");
				break;
			case ActionType.BIND_CHANGE_PHONE:// 绑定手机
				app.showMsg(flagStr + "手机成功！");
				CacheData.MOBILE_NUMBER = newMobile;
				finish();
				break;
		}
	}

	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.BIND_CHANGE_PHONE:
			case ActionType.SEND_AGAIN:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btn_bind_to_phone_code_send:// 发送验证码
				doSendVerification();
				break;
			case R.id.btn_bind_to_phone_code_submit:// 绑定手机
				doBindToPhone();
				break;
			default:
				break;
		}
	}
}
